package com.goddess.yeecolor.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.view.Titles;
import com.goddess.yeecolor.view.dialog_progress;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 发表评论
 */
public class CommitActivity extends BaseActivity{


    private View mView;
    private EditText commit_edittext;
    private TextView title_content;
    private int tag;
    private  Bundle bundle;
    private String typed,level,comment_id;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView= LayoutInflater.from(this).inflate(R.layout.layout_commit,null);
        setContentView(mView);
        bundle=getIntent().getBundleExtra("bundle");
        tag=bundle.getInt("tag");
        typed=bundle.getString("typed");
        level=bundle.getString("level");
        comment_id=bundle.getString("comment_id");
        initFindView();
    } private dialog_progress dialog_progress;

    private void setProgress(){
        dialog_progress=new dialog_progress(this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }

    @Override
    public void initOnClck() {

    }
    private boolean isLogin=false;

    private String content;
    @Override
    public void initFindView() {
        isLogin=SharePreceUtils.getBoolean("user","isLogin");
        commit_edittext= (EditText) findViewById(R.id.commit_edittext);
        Titles.initTitleLeft(mView, "关闭", (view)->{
            finish();
            overridePendingTransition(0,0);

        });
        Titles.initTitleRight(mView, "发送", (view)->{
            content=commit_edittext.getText().toString().trim();
            if (TextUtils.isEmpty(content)){

                Toast.makeText(CommitActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
            }
            else {
                if (isLogin){
                    send(content);
                }
                else {
                    JumpLogin();
                }


            }
        });
        Titles.initTitle(mView,"我要评论");
    }
    /**
     * 判断是否登录跳转登录界面
     */
    private void JumpLogin(){
        JumpIntents.startIntent(CommitActivity.this,LoginAcitivity.class);

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        isLogin=SharePreceUtils.getBoolean("user","isLogin");
    }
    private Map<Object,Object> map;
    private void setRequest(String url, Map<Object,Object> map, Handler handler, int tag){
        OkttpUtils.getIntence().OkttpPost(url,map,handler,tag);
    }

    private void setOk(String content,int tag){

        LogUtils.getInstence().info("content="+content+"level="+level+"typed="+typed+"comment_id="+comment_id);
        map=new HashMap<>();
        map.put("content",content);
        map.put("level",level);//所属评论
        map.put("typed",typed);//类型
        map.put("comment_id",comment_id);//评论id
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        setRequest(MyUrl.SENDCOMMIT,map,handler,tag);
        setProgress();
    }
    private  int position;
    private void send(String content){

        if (tag==1){
            /**
             * 发表评论
             */
        LogUtils.getInstence().info("内容"+content+"tag=1");
            setOk(content,1);

        }
        else if (tag==2){
            /**
             * 回复楼主
             */
            position=bundle.getInt("position",-1);

            setOk(content,2);


        }
        else if (tag==3){
            /**
             * 回复其他人
             */
            setOk(content,3);

        }
        else if (tag==4){
            Bundle bundel=getIntent().getBundleExtra("bundle");
            bundel.putString("content",content);
            JumpIntents.startIntentResult(this,4,bundel);
        }
        else if (tag==5){
            Bundle bundel=getIntent().getBundleExtra("bundle");
            bundel.putString("content",content);
            JumpIntents.startIntentResult(this,5,bundel);
        }
    }

    private String jsonString;
    private int code;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try{
                jsonString=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonString);
                code=object.getInt("code");

                if (code==100){
                    JSONObject result=object.getJSONObject("result");
                    if (msg.what==1){
//                        ToastUtils.toast(object.getString("extends"));
                        JSONObject user_info=result.getJSONObject("user_info");
//                        JSONObject basic=user_info.getJSONObject("basic");
//
                        Bundle bundle=new Bundle();
//                        bundle.putString("addtime",result.getString("addtime"));//添加时间
//                        bundle.putString("content",result.getString("content"));
//                        bundle.putString("thumb",basic.getString("thumb"));
//                        bundle.putString("zan",result.getString("zan"));
//                        bundle.putString("nickname",basic.getString("nickname"));


                            JumpIntents.startIntentResult(CommitActivity.this,1,bundle);


                    }
                    else if (msg.what==2){
                        ToastUtils.toast(object.getString("extends"));
                        JSONObject user_info=result.getJSONObject("user_info");
                        JSONObject basic=user_info.getJSONObject("basic");
                        Bundle bundle=new Bundle();
                        bundle.putString("addtime",result.getString("addtime"));//添加时间
                        bundle.putString("content",content);
                        bundle.putString("thumb",basic.getString("thumb"));
                        bundle.putInt("id",basic.getInt("id"));

                        bundle.putString("nickname",basic.getString("nickname"));

                            bundle.putInt("position",position);
                            JumpIntents.startIntentResult(CommitActivity.this,2,bundle);

                    }
                    else if (msg.what==3){
                        ToastUtils.toast(object.getString("extends"));
                        Bundle bundel=getIntent().getBundleExtra("bundle");
                        JSONObject user_info=result.getJSONObject("user_info");
                        JSONObject basic=user_info.getJSONObject("basic");
                        bundel.putString("addtime",result.getString("addtime"));//添加时间
                        bundel.putString("content",content);
                        bundel.putString("thumb",basic.getString("thumb"));
                        bundel.putInt("id",basic.getInt("id"));
                        JSONObject belong_info=result.getJSONObject("belong_info");

                        bundel.putString("content",content);

                            int position=bundel.getInt("parentPosition",-1);
                            int chid=bundel.getInt("childPosition",-1);
                            bundel.putInt("parentPosition",position);
                            bundel.putInt("childPosition",chid);
                            JumpIntents.startIntentResult(CommitActivity.this,3,bundel);


                    }
                    if (dialog_progress!=null&&dialog_progress.isShowing()){
                        dialog_progress.dismiss();
                    }
                }
                else if (code==403){
                    ToastUtils.toast(object.getString("msg"));
                }
            }
            catch (Exception e){

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog_progress!=null&&dialog_progress.isShowing()){
            dialog_progress.dismiss();
        }
    }
}
